﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Fitment.Admin
{
    public partial class managerMng : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindList();
            }
            if (Session["LoginUser"] == null)    //上面Session名字用LoginUser,这里的名字要和上面的相同,可以随便改,保持一致就行了,注意:Session["LoginUser"]后面,没有ToString()哦,你要是加上的话,就会出错了.
            {
                Response.Redirect("logon.aspx"); //如果Session为空,则转到登陆页.
            }
           
        }

        DS data = new DS();  //新建数据库连接
        string strAccount = null;
        string strPwd = null;

        protected void add_Button_Click(object sender, EventArgs e)
        {
            strAccount = this.acn_TextBox.Text.Trim(); //获取账号
            strPwd = this.pwd_TextBox.Text.Trim();   //获取密码
            string strSql = "SELECT *FROM Info WHERE RollerID='1' AND Account='" + strAccount + "'";      //查询账号是否已经存在
            if (strPwd==""||strAccount=="")
            {
                this.erro.Visible = true;
                this.erro.Text = "账号和密码不能为空";
                return;
            }
            erro.Visible = false;  
            DataTable table = data.GetDataSource(strSql, "Query");    //返回查询结果
            if (table.Rows.Count != 0)
            {
                //当查询管理员已存在时返回
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('管理员已存在，请重新选择！');</script>");
                return;
            }
            else
            {
                strSql = "INSERT INTO Info (RollerID,Account,Password)Values('1','"+strAccount+"','"+strPwd+"')";
                if (data.ExcuteSql(strSql))
                {
                    //当添加成功时返回
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert(插入成功！');</script>");
                    bindList();
                   
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('添加失败！');</script>");
                    bindList();
                    return;
                }

            }
           

        }
        
      
        protected void bindList()
        {
            string strSql = "SELECT *FROM Info WHERE RollerID='1'";
            DataTable table = data.GetDataSource(strSql, "Query");
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();
            this.acn_TextBox.Text = "";

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
              
            strAccount = (this.GridView1.Rows[e.RowIndex].FindControl("acn") as Label).Text.Trim();
            string strSql = String.Format("DELETE FROM Info WHERE Account='{0}' AND RollerID='1'", strAccount);
            if (data.ExcuteSql(strSql))
            {
                bindList();
            }
            else return;

       
        }
    }
}
